import axios from "axios";
import {stringify} from "qs";

// axios.defaults.baseURL = 'http://192.168.0.110:8080/'  //正式
axios.defaults.baseURL = 'http://127.0.0.1:8088/' //测试

//post请求头
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";
//设置超时
axios.defaults.timeout = 600 * 1000;
axios.defaults.withCredentials = true;

axios.interceptors.request.use(
    (config: any) => {
        return config;
    },
    (error: any) => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    (response: any) => {
        if (response.status == 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    (error: any) => {
        console.log(error);
    }
);
export default {
    post(url: string, data: any, headers: any) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data,
                headers: headers
            })
                .then((res: any) => {
                    resolve(res.data);
                })
                .catch((err: any) => {
                    console.log(err);
                    reject(err)
                });
        })
    },

    get(url: string, data: any = null) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url,
                params: data,
            })
                .then((res: any) => {
                    resolve(res.data)
                })
                .catch((err: any) => {
                    console.log(err);
                    reject(err)
                })
        })
    }
};

